<%-- 
    Document   : newLocCom
    Created on : Dec 13, 2009, 11:13:08 PM
    Author     : Jeremy
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="java.util.List" %>
<%@ page import="jhu.profiteer.data.Location" %>
<%@ page import="jhu.profiteer.data.LocationJpaController" %>
<%@ page import="jhu.profiteer.data.LocationCommodity" %>
<%@ page import="jhu.profiteer.data.LocationCommodityJpaController" %>
<%@ page import="jhu.profiteer.data.Commodity" %>
<%@ page import="jhu.profiteer.data.CommodityJpaController" %>

<%@ include file="/WEB-INF/jspf/admin/auth.jspf" %>
<html>
  <%
    String pageTitle = "Administration Homepage - Location Commodities";
    String cssFile = "/profiteer-game/css/profiteer.css";
  %>
  <%@ include file="/WEB-INF/jspf/admin/header.jspf" %>
  <body>
    <%@ include file="/WEB-INF/jspf/title.jspf" %>
    <div class="contentArea Body">
      <div class="pagetitle">Administration</div>
      <div class="pagebody">
        <table>
          <tr>
            <td id="menu"><%@ include file="/WEB-INF/jspf/admin/sidebar.jspf" %></td>
            <td>
              <div class="divround">
                <%@ include file="/WEB-INF/jspf/admin/notice.jspf" %>
                <%
                  notices = (Vector<String>)session.getAttribute("notices");
                  LocationJpaController ljc = new LocationJpaController();
                  String locIdParam = request.getParameter("locationId");

                  if((null == locIdParam) || locIdParam.isEmpty())
                  {
                    if(null == notices) notices = new Vector<String>();
                    notices.add("You must supply a location ID.");
                    session.setAttribute("notices", notices);
                    this.getServletContext()
                            .getRequestDispatcher("/admin/loccoms")
                            .forward(request, response);
                  }

                  Location l = ljc.findLocation(Long.parseLong(locIdParam));
                  CommodityJpaController cjc = new CommodityJpaController();
                  List<Commodity> commodities = null;
                  String comIdParam = request.getParameter("commodityId");
                  Commodity c = null;

                  if((null != comIdParam) && !comIdParam.isEmpty())
                    c = cjc.findCommodity(Long.parseLong(comIdParam));
                  else
                    commodities = cjc.findCommodityEntities();

                  if((null == c) &&
                          ((null == commodities) || (0 == commodities.size())))
                  {
                %>
                <p>There are no available commodities!</p>
                <%
                  }
                  else
                  {
                %>
                <form action="processLocCom" method="post">
                  <input type="hidden" name="locationId" value="<%= l.getId() %>" />
                  <table>
                    <tr>
                      <td>Location:</td>
                      <td><%= l.getName() %></td>
                    </tr>
                    <tr>
                      <td>Commodity:</td>
                  <%
                      LocationCommodity lc = null;

                      if(null != c)
                      {
                        LocationCommodityJpaController lcjc =
                                new LocationCommodityJpaController();
                        lc = lcjc.findLocationCommodity(l.getId(), c.getId());
                  %>
                      <input type="hidden" name="commodityId" value="<%= c.getId() %>" />
                      <td><%= c.getName() %></td>
                  <%
                      }
                      else
                      {
                  %>
                      <td>
                        <select name="commodityId">
                    <%
                        for(Commodity com : commodities)
                        {
                    %>
                          <option value="<%= com.getId() %>"><%= com.getName() %></option>
                    <%
                        }
                    %>
                        </select>
                      </td>
                    <%
                      }
                    %>
                    </tr>
                    <tr>
                      <td>Minimum Price:</td>
                      <td><input type="text" name="minPrice" <% if(null != lc) { %> value="<%= lc.getMinPrice() %>" <% } %>/></td>
                    </tr>
                    <tr>
                      <td>Maximum Price:</td>
                      <td><input type="text" name="maxPrice" <% if(null != lc) { %> value="<%= lc.getMaxPrice() %>" <% } %>/></td>
                    </tr>
                  </table>
                  <input type="submit" name="submit" <% if(null == lc) { %> value="Create" <% } else { %> value="Update" <% } %> />
                </form>
                <%
                  }
                %>
              </div>
            </td>
          </tr>
        </table>
      </div>
    </div>
    <%@ include file="/WEB-INF/jspf/footer.jspf" %>
  </body>
</html>